package com.fsp.jianchi.jgjplayer;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private IMusic music;
    ListView lv;
    LAdp ladp;

    private ServiceConnection sc;

    @Override
    protected void onDestroy(){
        if(music!=null)
            music.setNotificationEventListener(null);
        if(sc!=null)
            unbindService(sc);
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        sc = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                boolean isOk =false;
                if(iBinder!=null) {
                    music = (IMusic) iBinder;
                    if (music.isOk()) {
                        List<String> songsList = music.getMp3s();

                        if (songsList.size() == 0) {
                            setTitle("未找到MP3");
                            return;
                        }
                        else {
                            ladp = new LAdp(songsList, music.getMp3Id(), getApplicationContext());
                            lv = (ListView) findViewById(R.id.listView);
                            lv.setAdapter(ladp);

                            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    int mp3Id = (int) view.getTag();
                                    if (mp3Id != ladp.mp3Id) {
                                        ladp.mp3Id = mp3Id;
                                        ladp.notifyDataSetInvalidated();
                                        music.moveon(mp3Id, 0);
                                    }
                                }
                            });

                            //启动全局服务
                            Intent intent = new Intent(MainActivity.this, PlayerService.class);
                            startService(intent);

                            music.setNotificationEventListener(new NotificationEventListener() {
                                @Override
                                public void handleEvent() {
                                    ladp.mp3Id = music.getMp3Id();
                                    ladp.notifyDataSetChanged();
                                }
                            });

                            isOk = true;
                        }
                    }
                }
                if(!isOk){
                    setTitle("启动失败");
                    unbindService(sc);
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        };

        boolean isok = bindService(new Intent(this, PlayerService.class), sc, BIND_AUTO_CREATE);
        if(isok)
        {
            Button stop_button = (Button) findViewById(R.id.stop_button);
            Button play_button = (Button) findViewById(R.id.play_button);
            Button pause_button = (Button) findViewById(R.id.pause_button);

            pause_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(music!=null && music.isOk())
                        music.pause();
                }
            });

            stop_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(music!=null && music.isOk()) {
                        music.setNotificationEventListener(null);
                        if(sc!=null)
                            unbindService(sc);
                        Intent intent = new Intent(MainActivity.this, PlayerService.class);
                        stopService(intent);
                        sc=null;
                        music=null;
                    }
                    finish();
                }
            });

            play_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(music!=null && music.isOk())
                        music.play();
                }
            });
        }
    }
}
